package com.wbao.dianniu.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.customView.SpanTextView;
import com.wbao.dianniu.data.AdvertListData;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AdvertDelManager;
import com.wbao.dianniu.ui.AdInfoActivity;
import com.wbao.dianniu.ui.SendAdActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yejianjun on 2018/5/28.
 */

public class ExchangeHisAdapter extends BaseAdapter {

    private Context mContext;
    private List<AdvertListData> list = new ArrayList<>();
    private LayoutInflater inflater;
    private final int INIT = 1;
    private final int PASS = 2;
    private final int FAIL = 3;

    public ExchangeHisAdapter(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(context);
    }

    public void clear() {
        if (null != list) {
            list.clear();
        }
    }


    public void addData(List<AdvertListData> newData) {
        if (null == list) {
            list = new ArrayList<>();
        }
        for (int i = 0; i < newData.size(); i++) {
            list.add(newData.get(i));
        }
    }

    public void addFootOneData(AdvertListData data) {
        list.add(0, data);
        notifyDataSetChanged();
    }

    public void removeOneData(int id) {
        for (int i = 0; i < list.size(); i++) {
            if (id == list.get(i).getId()) {
                list.remove(i);
                break;
            }
        }
        notifyDataSetChanged();
    }

    public void modifyOneData(int id, AdvertListData data) {
        for (int i = 0; i < list.size(); i++) {
            if (id == list.get(i).getId()) {
                list.set(i, data);
                break;
            }
        }
        notifyDataSetChanged();
    }

    public List<AdvertListData> getList() {
        return list;
    }

    public void setData(List<AdvertListData> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.exchange_his_item, null);
            holder = new ViewHolder();
            holder.statusTV = (TextView) convertView.findViewById(R.id.exchange_result);
            holder.contentTV = (SpanTextView) convertView.findViewById(R.id.exchange_content_spanTV);
            holder.resumeTV = (TextView) convertView.findViewById(R.id.exchange_resume);
            holder.editTV = (TextView) convertView.findViewById(R.id.exchange_edit);
            holder.deleteTV = (TextView) convertView.findViewById(R.id.exchange_delete);
            holder.adIV = (ImageView) convertView.findViewById(R.id.exchange_ad_iv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final AdvertListData data = list.get(position);
        holder.contentTV.setText(data.getContent() == null ? "" : data.getContent());
        // 显示广告图片
        if (!TextUtils.isEmpty(data.getAdvertImage())) {   // 显示广告图
            Glide.with(mContext)
                    .load(GlobalContext.cdndownUrl + data.getAdvertImage())
                    .asBitmap()
                    .into(new AdViewTarget(holder.adIV));
        }
        MyClickListener clickListener = new MyClickListener(data);
        holder.adIV.setOnClickListener(clickListener);
        holder.contentTV.setOnClickListener(clickListener);
        int status = data.getStatus();
        if (INIT == status) {
            holder.statusTV.setText("审核中");
            holder.statusTV.setTextColor(mContext.getResources().getColor(R.color.red));
            holder.resumeTV.setVisibility(View.GONE);
            holder.editTV.setVisibility(View.GONE);
            holder.deleteTV.setVisibility(View.GONE);
            holder.statusTV.setClickable(false);
        } else if (PASS == status) {
            holder.statusTV.setText("审核通过");
            holder.statusTV.setTextColor(mContext.getResources().getColor(R.color.blue_text));
            holder.resumeTV.setVisibility(View.GONE);
            holder.editTV.setVisibility(View.GONE);
            holder.deleteTV.setVisibility(View.GONE);
            holder.statusTV.setClickable(false);
        } else if (FAIL == status) {
            holder.statusTV.setText("【原因】");
            holder.statusTV.setTextColor(mContext.getResources().getColor(R.color.blue_text));
            holder.resumeTV.setVisibility(View.VISIBLE);
            holder.editTV.setVisibility(View.VISIBLE);
            holder.deleteTV.setVisibility(View.VISIBLE);
            holder.statusTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 显示拒绝原因
                    CustomDialog.Builder build = new CustomDialog.Builder(mContext);
                    build.setTitle(data.getAuditContent());
                    build.setPositiveButton(mContext.getResources().getString(R.string.confirm),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    build.create().show();
                }
            });
            holder.editTV.setOnClickListener(clickListener);
            holder.deleteTV.setOnClickListener(clickListener);
        }

        return convertView;
    }

    class ViewHolder {
        private TextView statusTV;
        private TextView contentTV;
        private TextView resumeTV;
        private TextView editTV;
        private TextView deleteTV;
        private ImageView adIV;
    }


    class MyClickListener implements View.OnClickListener {
        private AdvertListData advertListData;

        public MyClickListener(AdvertListData data) {
            this.advertListData = data;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.exchange_edit:
                    Bundle bundle = new Bundle();
                    bundle.putInt(Const.BUNDLE_ID, advertListData.getId());
                    bundle.putString(Const.BUNDLE_CONTENT, advertListData.getContent());
                    bundle.putString(Const.BUNDLE_HEADPIC, advertListData.getAdvertImage());
                    bundle.putString(Const.BUNDLE_ADVERTTOPAGEURL, advertListData.getToPageUrl());
                    Intent intent = new Intent();
                    intent.putExtras(bundle);
                    intent.setClass(mContext, SendAdActivity.class);
                    mContext.startActivity(intent);
                    break;
                case R.id.exchange_delete:
                    delHisAdvert(advertListData.getId());
                    break;
                case R.id.exchange_ad_iv:
                case R.id.exchange_content_spanTV:
                    Intent adIntent = new Intent();
                    adIntent.putExtra(Const.BUNDLE_DATA, advertListData.getToPageUrl());
                    adIntent.setClass(mContext, AdInfoActivity.class);
                    mContext.startActivity(adIntent);
                    break;
                default:
                    break;
            }
        }
    }


    private void delHisAdvert(final Integer id) {
        CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
        builder.setTitle("确认删除改条广告吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                AdvertDelManager delManager = new AdvertDelManager(mContext);
                delManager.advertDel(id);
                removeOneData(id);
            }
        });
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();

    }

}
